<template>
  <div class="data-review-container">
    <el-card class="header-card">
      <div class="header-content">
        <h2>数据审核</h2>
        <div class="header-stats">
          <el-tag type="warning" size="large" effect="dark">待审核: {{ totalPendingCount }}</el-tag>
        </div>
      </div>
    </el-card>

    <div v-loading="loading" class="review-content">
      <!-- 论文审核卡片 -->
      <el-card v-if="pendingData.paperList && pendingData.paperList.length > 0" class="review-card">
        <template #header>
          <div class="card-header">
            <span>待审核论文 ({{ pendingData.paperList.length }})</span>
          </div>
        </template>
        <el-table :data="pendingData.paperList" border style="width: 100%">
          <el-table-column prop="id" label="ID" width="80" align="center"></el-table-column>
          <el-table-column prop="title" label="论文标题" min-width="200" show-overflow-tooltip></el-table-column>
          <el-table-column prop="journal" label="期刊" min-width="150" show-overflow-tooltip></el-table-column>
          <el-table-column prop="impactFactor" label="影响因子" width="100" align="center"></el-table-column>
          <el-table-column prop="publishDate" label="发表日期" width="120" align="center"></el-table-column>
          <el-table-column label="操作" width="200" align="center">
            <template #default="scope">
              <div class="operation-buttons">
                <el-button type="success" size="small" @click="handleStatusChange('paper', scope.row.id, 'approved')">
                  通过
                </el-button>
                <el-button type="danger" size="small" @click="handleStatusChange('paper', scope.row.id, 'rejected')">
                  拒绝
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </el-card>

      <!-- 科研项目审核卡片 -->
      <el-card v-if="pendingData.researchProjectList && pendingData.researchProjectList.length > 0" class="review-card">
        <template #header>
          <div class="card-header">
            <span>待审核科研项目 ({{ pendingData.researchProjectList.length }})</span>
          </div>
        </template>
        <el-table :data="pendingData.researchProjectList" border style="width: 100%">
          <el-table-column prop="id" label="ID" width="80" align="center"></el-table-column>
          <el-table-column prop="name" label="项目名称" min-width="200" show-overflow-tooltip></el-table-column>
          <el-table-column prop="type" label="项目类型" width="120" align="center">
            <template #default="scope">
              <el-tag :type="getProjectTypeTag(scope.row.type)">{{ getProjectTypeText(scope.row.type) }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="funding" label="经费(万元)" width="120" align="center"></el-table-column>
          <el-table-column label="项目周期" width="200" align="center">
            <template #default="scope">
              {{ scope.row.startDate }} 至 {{ scope.row.endDate }}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200" align="center">
            <template #default="scope">
              <div class="operation-buttons">
                <el-button type="success" size="small" @click="handleStatusChange('project', scope.row.id, 'approved')">
                  通过
                </el-button>
                <el-button type="danger" size="small" @click="handleStatusChange('project', scope.row.id, 'rejected')">
                  拒绝
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </el-card>

      <!-- 专利审核卡片 -->
      <el-card v-if="pendingData.patentList && pendingData.patentList.length > 0" class="review-card">
        <template #header>
          <div class="card-header">
            <span>待审核专利 ({{ pendingData.patentList.length }})</span>
          </div>
        </template>
        <el-table :data="pendingData.patentList" border style="width: 100%">
          <el-table-column prop="id" label="ID" width="80" align="center"></el-table-column>
          <el-table-column prop="name" label="专利名称" min-width="200" show-overflow-tooltip></el-table-column>
          <el-table-column prop="type" label="专利类型" width="120" align="center">
            <template #default="scope">
              <el-tag :type="getPatentTypeTag(scope.row.type)">{{ getPatentTypeText(scope.row.type) }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="authorizationNumber" label="授权号" min-width="150" show-overflow-tooltip></el-table-column>
          <el-table-column prop="authorizationDate" label="授权日期" width="120" align="center"></el-table-column>
          <el-table-column label="操作" width="200" align="center">
            <template #default="scope">
              <div class="operation-buttons">
                <el-button type="success" size="small" @click="handleStatusChange('patent', scope.row.id, 'approved')">
                  通过
                </el-button>
                <el-button type="danger" size="small" @click="handleStatusChange('patent', scope.row.id, 'rejected')">
                  拒绝
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </el-card>

      <!-- 奖项审核卡片 -->
      <el-card v-if="pendingData.awardList && pendingData.awardList.length > 0" class="review-card">
        <template #header>
          <div class="card-header">
            <span>待审核奖项 ({{ pendingData.awardList.length }})</span>
          </div>
        </template>
        <el-table :data="pendingData.awardList" border style="width: 100%">
          <el-table-column prop="id" label="ID" width="80" align="center"></el-table-column>
          <el-table-column prop="name" label="奖项名称" min-width="200" show-overflow-tooltip></el-table-column>
          <el-table-column prop="level" label="奖项级别" width="120" align="center">
            <template #default="scope">
              <el-tag :type="getAwardLevelTag(scope.row.level)">{{ getAwardLevelText(scope.row.level) }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="issuingUnit" label="颁发单位" min-width="150" show-overflow-tooltip></el-table-column>
          <el-table-column prop="awardDate" label="获奖日期" width="120" align="center"></el-table-column>
          <el-table-column label="操作" width="200" align="center">
            <template #default="scope">
              <div class="operation-buttons">
                <el-button type="success" size="small" @click="handleStatusChange('award', scope.row.id, 'approved')">
                  通过
                </el-button>
                <el-button type="danger" size="small" @click="handleStatusChange('award', scope.row.id, 'rejected')">
                  拒绝
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </el-card>

      <!-- 无待审核数据时显示 -->
      <el-empty v-if="totalPendingCount === 0" description="暂无待审核数据"></el-empty>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import axios from 'axios'

// 待审核数据
const pendingData = ref({
  paperList: [],
  researchProjectList: [],
  patentList: [],
  awardList: []
})

const loading = ref(false)

// 计算待审核总数
const totalPendingCount = computed(() => {
  return (pendingData.value.paperList?.length || 0) +
         (pendingData.value.researchProjectList?.length || 0) +
         (pendingData.value.patentList?.length || 0) +
         (pendingData.value.awardList?.length || 0)
})

// 获取项目类型标签
const getProjectTypeTag = (type) => {
  const typeMap = {
    'national': 'danger',
    'provincial': 'warning',
    'municipal': 'success',
    'horizontal': 'info'
  }
  return typeMap[type] || 'info'
}

// 获取项目类型文本
const getProjectTypeText = (type) => {
  const typeMap = {
    'national': '国家级',
    'provincial': '省级',
    'municipal': '市级',
    'horizontal': '横向'
  }
  return typeMap[type] || type
}

// 获取专利类型标签
const getPatentTypeTag = (type) => {
  const typeMap = {
    'invention': 'danger',
    'utility_model': 'warning',
    'design': 'success'
  }
  return typeMap[type] || 'info'
}

// 获取专利类型文本
const getPatentTypeText = (type) => {
  const typeMap = {
    'invention': '发明专利',
    'utility_model': '实用新型',
    'design': '外观设计'
  }
  return typeMap[type] || type
}

// 获取奖项级别标签
const getAwardLevelTag = (level) => {
  const levelMap = {
    'national': 'danger',
    'provincial': 'warning',
    'municipal': 'success',
    'school': 'info'
  }
  return levelMap[level] || 'info'
}

// 获取奖项级别文本
const getAwardLevelText = (level) => {
  const levelMap = {
    'national': '国家级',
    'provincial': '省级',
    'municipal': '市级',
    'school': '校级'
  }
  return levelMap[level] || level
}

// 获取待审核数据列表
const fetchPendingList = async () => {
  loading.value = true
  try {
    const response = await axios.get('http://localhost:8081/api/process/list')
    if (response.data.code === 200) {
      pendingData.value = response.data.data['pending-list'] || {
        paperList: [],
        researchProjectList: [],
        patentList: [],
        awardList: []
      }
    } else {
      ElMessage.error(response.data.msg || '获取待审核列表失败')
    }
  } catch (error) {
    console.error('获取待审核列表错误:', error)
    ElMessage.error('获取待审核列表失败，请检查网络连接')
  } finally {
    loading.value = false
  }
}

// 处理状态变更
const handleStatusChange = async (type, id, status) => {
  const actionText = status === 'approved' ? '通过' : '拒绝'
  
  try {
    await ElMessageBox.confirm(
      `确定要${actionText}此条记录吗？`,
      '确认操作',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: status === 'approved' ? 'success' : 'warning'
      }
    )
    
    const params = new URLSearchParams()
    params.append('type', type)
    params.append('id', id)
    params.append('status', status)
    
    const response = await axios.post('http://localhost:8081/api/process/change', params)
    
    if (response.data.code === 200) {
      ElMessage.success(`${actionText}成功`)
      fetchPendingList() // 刷新列表
    } else {
      ElMessage.error(response.data.msg || `${actionText}失败`)
    }
  } catch (error) {
    if (error !== 'cancel') {
      console.error('状态变更错误:', error)
      ElMessage.error(`${actionText}失败，请检查网络连接`)
    }
  }
}

// 页面加载时获取待审核列表
onMounted(() => {
  fetchPendingList()
})
</script>

<style scoped>
.data-review-container {
  padding: 20px;
}

.header-card {
  margin-bottom: 20px;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.review-content {
  min-height: 300px;
}

.review-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.operation-buttons {
  display: flex;
  justify-content: center;
  gap: 10px;
}

:deep(.el-table) {
  border-radius: 4px;
  overflow: hidden;
}

:deep(.el-table th) {
  background-color: #f5f7fa;
  color: #606266;
  font-weight: bold;
}

:deep(.el-table--border .el-table__cell) {
  border-right: 1px solid #ebeef5;
}

:deep(.el-table__row:hover td) {
  background-color: #f5f7fa;
}
</style>